package com.shipment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shipment.common.Result;
import com.shipment.entity.DeliveryOrder;
import com.shipment.mapper.DeliveryOrderMapper;
import com.shipment.model.delivery.DeliveryOrderDTO;
import com.shipment.model.delivery.DeliveryOrderQuery;
import com.shipment.service.DeliveryOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发货订单服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeliveryOrderServiceImpl extends ServiceImpl<DeliveryOrderMapper, DeliveryOrder> implements DeliveryOrderService {

    /**
     * 创建发货订单
     *
     * @param orderDTO 发货订单DTO
     * @return 操作结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result createDeliveryOrder(DeliveryOrderDTO orderDTO) {
        // 数据校验
        if (orderDTO == null) {
            return Result.error("参数不能为空");
        }
        if (StrUtil.isBlank(orderDTO.getProjectName())) {
            return Result.error("发货项目名称不能为空");
        }
        if (orderDTO.getDeliveryDate() == null) {
            return Result.error("发货时间不能为空");
        }
        if (StrUtil.isBlank(orderDTO.getClientAddress())) {
            return Result.error("甲方地址不能为空");
        }
        if (StrUtil.isBlank(orderDTO.getClientContact())) {
            return Result.error("甲方联系人不能为空");
        }
        if (StrUtil.isBlank(orderDTO.getClientPhone())) {
            return Result.error("甲方电话不能为空");
        }

        // 复制DTO到实体
        DeliveryOrder deliveryOrder = new DeliveryOrder();
        BeanUtil.copyProperties(orderDTO, deliveryOrder);

        // 初始状态为待审核
        deliveryOrder.setOrderStatus(0);

        // 保存
        save(deliveryOrder);
        return Result.success("创建发货订单成功");
    }

    /**
     * 更新发货订单
     *
     * @param orderDTO 发货订单DTO
     * @return 操作结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateDeliveryOrder(DeliveryOrderDTO orderDTO) {
        // 数据校验
        if (orderDTO == null || orderDTO.getId() == null) {
            return Result.error("参数错误");
        }

        // 查询原订单
        DeliveryOrder original = getById(orderDTO.getId());
        if (original == null) {
            return Result.error("发货订单不存在");
        }

        // 如果订单已审核，则不允许修改
        if (original.getOrderStatus() == 1) {
            return Result.error("已审核的订单不允许修改");
        }

        // 复制DTO到实体
        DeliveryOrder deliveryOrder = new DeliveryOrder();
        BeanUtil.copyProperties(orderDTO, deliveryOrder);

        // 如果之前是已审核状态，现在修改了，状态变为已修改待审核
        if (original.getOrderStatus() == 1) {
            deliveryOrder.setOrderStatus(2); // 已修改待审核
        }

        // 更新
        updateById(deliveryOrder);
        return Result.success("更新发货订单成功");
    }

    /**
     * 根据ID获取发货订单详情
     *
     * @param id 发货订单ID
     * @return 操作结果
     */
    @Override
    public Result getDeliveryOrderById(Long id) {
        if (id == null) {
            return Result.error("参数错误");
        }
        DeliveryOrder deliveryOrder = getById(id);
        if (deliveryOrder == null) {
            return Result.error("发货订单不存在");
        }
        return Result.success(deliveryOrder);
    }

    /**
     * 删除发货订单
     *
     * @param id 发货订单ID
     * @return 操作结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteDeliveryOrder(Long id) {
        if (id == null) {
            return Result.error("参数错误");
        }

        // 查询订单
        DeliveryOrder deliveryOrder = getById(id);
        if (deliveryOrder == null) {
            return Result.error("发货订单不存在");
        }

        // 已审核的订单不允许删除
        if (deliveryOrder.getOrderStatus() == 1) {
            return Result.error("已审核的订单不允许删除");
        }

        // 删除
        removeById(id);
        return Result.success("删除发货订单成功");
    }

    /**
     * 分页查询发货订单
     *
     * @param query 查询条件
     * @return 分页结果
     */
    @Override
    public IPage<DeliveryOrder> pageDeliveryOrders(DeliveryOrderQuery query) {
        Page<DeliveryOrder> page = new Page<>(query.getPageNum(), query.getPageSize());
        return baseMapper.selectDeliveryOrderPage(page, query);
    }

    /**
     * 更新订单状态
     *
     * @param id     订单ID
     * @param status 订单状态
     * @return 操作结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateOrderStatus(Long id, Integer status) {
        if (id == null || status == null) {
            return Result.error("参数错误");
        }

        // 查询订单
        DeliveryOrder deliveryOrder = getById(id);
        if (deliveryOrder == null) {
            return Result.error("发货订单不存在");
        }

        // 状态只能是0(待审核)、1(已审核)、2(已修改待审核)
        if (status < 0 || status > 2) {
            return Result.error("状态参数错误");
        }

        // 更新状态
        DeliveryOrder update = new DeliveryOrder();
        update.setId(id);
        update.setOrderStatus(status);
        updateById(update);

        return Result.success("更新订单状态成功");
    }
}
